package cn.tedu.csmallpassport.controller;

import cn.tedu.csmallpassport.ex.ServiceException;
import cn.tedu.csmallpassport.pojo.dto.AdminAddNewDTO;
import cn.tedu.csmallpassport.pojo.dto.AdminLoginInfoDTO;
import cn.tedu.csmallpassport.pojo.vo.AdminListItemVO;
import cn.tedu.csmallpassport.security.AdminDetails;
import cn.tedu.csmallpassport.security.LoginPrincipal;
import cn.tedu.csmallpassport.service.AdminService;
import cn.tedu.csmallpassport.web.JsonResult;
import cn.tedu.csmallpassport.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
@Api(tags = "01. 管理员模块")
public class AdminController {

    @Autowired
    AdminService service;

    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    public JsonResult<Void> addNew(AdminAddNewDTO adminAddNewDTO){
        service.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除管理员")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1) Long id){
        service.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("管理员列表")
    @ApiOperationSupport(order = 320)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> select(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        List<AdminListItemVO> select = service.list();
        return JsonResult.ok(select);
    }

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 10)
    public JsonResult<String> login(AdminLoginInfoDTO adminLoginInfoDTO) {
        String jwt = service.login(adminLoginInfoDTO);
        return JsonResult.ok(jwt);
    }

//    http://localhost:8080/admin/4/disable
    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("管理员禁用")
//    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperationSupport(order = 20)
    public JsonResult<Void> disableEnable(@PathVariable @Range(min = 1)Long id){
        if (id == 1L){
            String message = "系统管理员不可被修改";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        service.disable(id);
        return JsonResult.ok();
    }

//    http://localhost:8080/admin/2/enable
    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("管理员启用")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiOperationSupport(order = 30)
    public JsonResult<Void> Enable(@PathVariable @Range(min = 1)Long id){
        if (id == 1L){
            String message = "系统管理员不可被修改";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        service.enable(id);
        return JsonResult.ok();
    }
}
